import { updateUserArea, updateUserSchool } from '../../../api/user'
import { areaList } from '../../../api/base'
const app = getApp()
Page({
  data: {
    content: '',

    /** 省市区参数 **/
    areaCode: '',
    province_list: null,
    province_name: null,
    city_list: null,
    city_name: null,
    area_list: null,
    area_name: null,
    addressCity: null,
    multiArray: [],  // 三维数组数据
    multiIndex: [0, 0, 0], // 默认的下标,
    selectProvinceId: null,
    selectCityId: null,
    selectAreaId: null
    /** 省市区参数 **/
  },

  onLoad: function (options) {
    this.setData({
      content: options.key,
      areaCode: !app.globalData.area.areaCode?'':app.globalData.area.areaCode
    })

    // 城市参数获取
    this.getProvince()
    this.setData({
      addressCity: [
        !app.globalData.area.provinceName?'':app.globalData.area.provinceName,
        !app.globalData.area.cityName?'':app.globalData.area.cityName,
        !app.globalData.area.districtName?'':app.globalData.area.districtName
      ]
    })
  },

  /** 省市区 start **/
  //获取省份列表
  getProvince(){
    let that = this
    areaList({}).then(res => {
      let provinceList = [...res]  //放到一个数组里面
      let provinceArr = res.map((item)=>{return item.name})  //获取名称
      that.setData({
        multiArray: [provinceArr,[],[]], //更新三维数组  更新完为[['广东','北京'],[],[]]
        province_list: provinceList,  //省级原始数据
        province_name: provinceArr,  //省级全部名称
      })
      let defaultCode = that.data.province_list[0].code //使用第一项当作参数获取市级数据
      if(defaultCode){
        that.setData({
          currentProvinceId: defaultCode  //保存当前省份id
        })
        that.getCity(defaultCode) //获取市区数据
      }
    })
  },

  //根据省份id获取城市
  getCity(code){
    let that = this
    that.setData({
      currentProvinceId: code
    })

    areaList({ parentCode: code }).then(res => {
      let cityArr = res.map((item)=>{return item.name}) //返回城市名称
      let cityList = [...res]
      that.setData({
        multiArray:[that.data.province_name,cityArr,[]],  //更新后[['广东','北京'],['潮州'，'汕头','揭阳'],[]]
        city_list:cityList, //保持市级数据
        city_name:cityArr   //市级名称
      })
      let defaultCode = that.data.city_list[0].code //获取第一个市的区级数据
      if(defaultCode){
        that.setData({
          currentCityId:defaultCode //保存当下市id
        })
        that.getArea(defaultCode) //获取区域数据
      }
    })
  },

  //获取区域
  getArea:function(code){
    let that = this
    that.setData({
      currentCityId:code    //保存当前选择市
    })
    areaList({ parentCode: code }).then(res => {
      let areaList = [...res]
      let areaArr = res.map((item)=>{return item.name}) //区域名
      that.setData({
        multiArray:[that.data.province_name,that.data.city_name,areaArr],
        area_list:areaList, //区列表
        area_name:areaArr   //区名字
      })
    })
  },

  //picker确认选择地区
  bindRegionChange:function(e){
    const _this = this
    updateUserArea(_this.data.area_list[e.detail.value[2]].code).then(res => {
      _this.setData({
        areaCode: _this.data.area_list[e.detail.value[2]].code,
        multiIndex: e.detail.value,  //更新下标
        addressCity: [_this.data.province_list[e.detail.value[0]].name, _this.data.city_list[e.detail.value[1]].name, _this.data.area_list[e.detail.value[2]].name],
        selectProvinceId: _this.data.province_list[e.detail.value[0]].code,
        selectCityId: _this.data.city_list[e.detail.value[1]].code,
        selectAreaId: _this.data.area_list[e.detail.value[2]].code,
      })
    })
  },

  //滑动地区组件
  bindRegionColumnChange:function(e){
    // console.log(e.detail.column,e.detail.value)
    let that = this
    let column = e.detail.column  //当前改变的列
    let data = {
      multiIndex:JSON.parse(JSON.stringify(that.data.multiIndex)),
      multiArray:JSON.parse(JSON.stringify(that.data.multiArray))
    }
    data.multiIndex[column] = e.detail.value  //第几列改变了就是对应multiIndex的第几个，更新他
    switch(column){
      case 0 :  //第一列改变，省级改变
        let currentProvinceId = that.data.province_list[e.detail.value].code
        if(currentProvinceId != that.data.currentProvinceId){ //判断当前id是不是更新了
          that.getCity(currentProvinceId)   //获取当前id下的市级数据
        }
        data.multiIndex[1] = 0    //将市默认选择第一个
        break
      case 1 :  //第二列改变，市级改变
        let currentCityId = that.data.city_list[e.detail.value].code
        if(currentCityId != that.data.currentCityId){
          that.getArea(currentCityId) //获取区域
        }
        data.multiIndex[2] = 0  //区域默认第一个
        break
    }
    that.setData(data)  //更新数据
  },
  /** 省市区 end **/


  onblur(e) {
    this.setData({
      content: e.detail.value
    })
  },

  done(e) {
    const pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];
    updateUserSchool({
      schoolName: this.data.content,
      areaCode: this.data.areaCode
    }).then(res => {
      prevPage.setData({
        'form.school': this.data.content,
        'form.area.cityCode': this.data.selectProvinceId,
        'form.area.cityName': this.data.addressCity[0],
        'form.area.districtCode': this.data.selectCityId,
        'form.area.districtName': this.data.addressCity[1],
        'form.area.provinceCode': this.data.areaCode,
        'form.area.provinceName': this.data.addressCity[2]
      })
      wx.navigateBack({
        delta: 1,
      })
    })
  }
})
